package com.sdkx.app.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.ViewModel.AirWayManager;
import com.sdkx.app.ViewModel.MarkManager;
import com.sdkx.app.ViewModel.WayPointManager;
import com.sdkx.app.adapter.SearchAdapter;
import com.sdkx.app.model.WayPoint;
import com.sdkx.app.model.AirWay;
import com.sdkx.app.model.Mark;
import com.sdkx.app.model.ViewObject;
import com.sdkx.app.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * created by bjhero
 * changed by zd
 */
public class SearchDialog extends Dialog {
    private static final String TAG = SearchDialog.class.getSimpleName();
    private Context context;
    private final String title;
    private List<ViewObject> listDataSource = new ArrayList<>();
    private SearchAdapter searchAdapter;
    private RadioGroup radioGroup;
    private EditText editText;

    public SearchDialog(Context context,String title) {
        super(context, R.style.AppTheme );
        this.context = context;
        this.title = title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //点击dialog外部关闭
        setCanceledOnTouchOutside(true);
        initView();
    }

    public void initView() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dlg_search, null);
        setContentView(view);

        TextView tvTitle = view.findViewById(R.id.textView_title);
        tvTitle.setText(title);

        editText = view.findViewById(R.id.editText_search);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if(i == EditorInfo.IME_ACTION_SEARCH){
                    search();
                }
                return false;
            }
        });

        Button buttonSearch = view.findViewById(R.id.button_search);
        buttonSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                search();
            }
        });

        final ListView listView = view.findViewById(R.id.listview);
        searchAdapter = new SearchAdapter(getContext(),R.layout.search_listview_item,listDataSource);
        listView.setAdapter(searchAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                onListViewItemClick(view,position);
            }
        });

        radioGroup = view.findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                search();
            }
        });

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        //lp.x=-80;
        lp.width = (int) (d.widthPixels * 0.6); // 高度设置为屏幕的0.6
        lp.height = (int) (d.heightPixels * 0.4); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    private void search(){
        final String searchText = editText.getText().toString();
        if(TextUtils.isEmpty(searchText)){
            return;
        }
        listDataSource.clear();
        switch (radioGroup.getCheckedRadioButtonId()){
            case R.id.radioBtn_all:
                listDataSource.addAll(getLikeAllTypes(searchText));
                break;
            case R.id.radioBtn_airport:
                listDataSource.addAll(WayPointManager.getAirportsLike(searchText));
                break;
            case R.id.radioBtn_waypoint:
                listDataSource.addAll(WayPointManager.getPointLike(searchText));
                break;
            case R.id.radioBtn_way:
                listDataSource.addAll(AirWayManager.getItems(searchText));
                break;
            case R.id.radioBtn_mark:
                listDataSource.addAll(MarkManager.getItems(searchText));
                break;
            default:
                break;
        }
        searchAdapter.setSelectPosition(-1);
        searchAdapter.notifyDataSetInvalidated();
        if(listDataSource.isEmpty()){
            ToastUtil.show(getContext(),"无对应结果！");
        }
    }

    private void onListViewItemClick(View view, final int position) {
        searchAdapter.setSelectPosition(position);
        searchAdapter.notifyDataSetInvalidated();
    }

    //获取所有有关的对象
    private List<ViewObject> getLikeAllTypes(String keyWord) {
        List<ViewObject> resultList = new ArrayList<>();
        //检查点对象
        List<WayPoint> points = WayPointManager.getItems(keyWord);
        //检查线对象
        List<AirWay> ways = AirWayManager.getItems(keyWord);
        //检查标记对象
        List<Mark> marks = MarkManager.getItems(keyWord);
        resultList.addAll(points);
        resultList.addAll(ways);
        resultList.addAll(marks);
        return resultList;
    }
}